
<template>
  <div style="width: 430px;height: 932px;">
    <van-form @submit="user">
      <van-field
        v-model="user.username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="user.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <van-field
        v-model="user.yzm"
        type="验证码"
        name="验证码"
        label="验证码"
        placeholder="验证码"
        :rules="[{ required: true, message: '请填写验证码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" @click="tologin()">提交</van-button>
      </div>
    </van-form>
    <van-button round block type="info" native-type="submit" @click="toyzm()" :disabled="isDisabled">发送验证码</van-button>
    <p>----------------------------------------------------------------------------</p>
    <van-button round block type="info" native-type="submit" @click="tozuce()">还没有账号？去注册</van-button>
  </div>

</template>

<script>

import axios from "axios";

export default {
  data () {
    return {
      user:{},
      yzmId:0,
      yzmIdd:0,
      isDisabled:false,
      timer: null,
    }
  },
  methods: {
    tozuce(){
      this.$router.push('/register')
    },
    toyzm(){
      axios.post("/admin/sendyzm?yzm="+this.yzmId).then(res=>{
        localStorage.setItem("yzm",res.data.data.yzm)
        this.$toast("验证码发送成功"+"("+res.data.data.yzm+")")
        this.isDisabled = true
        this.timer = setTimeout(() => {
          this.isDisabled = false;
        }, 5 * 1000);
      })
    },
    tologin(){
      axios.post("/admin/login",this.user).then(res=>{
        this.yzmIdd = localStorage.getItem("yzm")
        if(res.data.code==200&&this.yzmIdd==this.user.yzm){
          localStorage.setItem("admin",JSON.stringify(res.data.data.admin))
          if(res.data.data.admin.level=="普通用户"){
            localStorage.setItem("adminId",res.data.data.admin.id)
          }
          this.$router.push('/shouyei')
        }else{
          this.$toast("登录失败")
        }
      })
    }
  }
}
</script>


<style scoped>

</style>
